package com.ricchieri.manage.bean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.SelectEvent;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.common.CustomScheduleEvent;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.model.Reservation;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.model.User;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "editReservationMB")
@ViewScoped
public class EditReservationManageBean extends CommonReservationManageBean {

	private static final long serialVersionUID = 5531296565698443473L;
	
	private Reservation reservation;
	private ReservationFilter reservationFilter;
	
	@PostConstruct
	public void init() {
		reservation = (Reservation) FacesUtil.getParametersFlash(BusinessSettings.KEY_RESERVATION);
		reservationFilter = (ReservationFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(reservation == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_RESERVATION);
		} else {
			setSelectedReservationType(String.valueOf(reservation.getReservationType().getId()));
			setSelectedPassengerType(String.valueOf(reservation.getPassengerType().getId()));
			setSelectedMeanPayment(String.valueOf(reservation.getMeanPayment().getId()));
		}
	}

	public Reservation getReservation() {
		return reservation;
	}

	public void setReservation(Reservation reservation) {
		this.reservation = reservation;
	}
	
	public String doBackToSearchReservation() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, reservationFilter);
		return BusinessSettings.SEARCH_RESERVATION;
	}

	
	public String saveReservation() {

		ScheduleCharterFilter filter = new ScheduleCharterFilter();
		filter.setId(reservation.getScheduleCharter().getId());
		filter.setTrip(String.valueOf(reservation.getScheduleCharter().getTrip().getId()));
		setScheduleCharter(getScheduleCharterService().getScheduleCharterByFilter(filter));
			
		if(getScheduleCharter().getCharter() != null && getScheduleCharter().getQuantityReserved() >= getScheduleCharter().getCharter().getPlaces()) {
			FacesUtil.showFatalMessage(getFormName(), "noCapacity");
			return BusinessSettings.UNSUCCESS;
		} else if (this.haveReservation(reservation.getUser(), reservation)) {
			FacesUtil.showFatalMessage(getFormName(), "reservationExist");
			return BusinessSettings.UNSUCCESS;
		} else {
			this.completeReservationTypes();
			getReservationService().updateReservation(reservation, Boolean.TRUE);
			FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
			
			return BusinessSettings.SUCCESS;
		}
	}
	
	protected String getFormName() {
		return "editReservation";
	}

	@Override
	public void onEventSelect(SelectEvent selectEvent) {
		CustomScheduleEvent event = (CustomScheduleEvent) selectEvent.getObject();
		ScheduleCharter oldScheduleCharter = reservation.getScheduleCharter();
		
		this.setScheduleCharter(event.getScheduleCharter());
		
		if(!this.haveReservation(reservation.getUser())) {
			if(reservation.getScheduleCharter().getTrip().getId() != getScheduleCharterFilter().getTripId()) {
				//TODO HARDCODE
				if(reservation.getScheduleCharter().getTrip().getId() == 1) {
					reservation.setDownOn("Obelisco/Diagonal");
					reservation.setUpOn("Alvear 534 (Oficina)");			
				} else {
					reservation.setUpOn("Obelisco/Diagonal");
					reservation.setDownOn("Alvear 534 (Oficina)");
				}
				//TODO HARDCODE
			} 
			reservation.setScheduleCharter(event.getScheduleCharter());
		} else {
			FacesUtil.showFatalMessage("messagesForm", "reservationExist");
			setScheduleCharter(oldScheduleCharter);
		}	
	}
	
	public Boolean haveReservation(User user, Reservation reser) {
		for (Reservation reservation : getScheduleCharter().getActiveReservations()) {
			if(reservation.getUser().getId() == user.getId() && reservation.getId() != reser.getId()
					&& reservation.getScheduleCharter().getId() != reser.getScheduleCharter().getId()) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}
}
